package com.snailct.bpp.router.datachange;

import java.sql.*;
import java.util.List;

/**
 * Created by huangxa on 2016/5/21.
 */
public class DaoOperation {
    public static void execute(String sql){
        Connection conn = null;
        PreparedStatement st = null;

        try {
            conn = DataSourceManager.getConn();
            st = conn.prepareStatement(sql);
            st.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if(st!=null){
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void execute(String sql,List<String> params){
        if(params==null){
            execute(sql);
            return;
        }
        Connection conn = null;
        PreparedStatement st = null;

        try {
            conn = DataSourceManager.getConn();
            st = conn.prepareStatement(sql);
            for(int i=0;i<params.size();i++){
                st.setString(i+1,params.get(i));
            }

            st.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if(st!=null){
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static ResultSet executeQuery(String sql) {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            conn = DataSourceManager.getConn();
            st = conn.createStatement();
            rs = st.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {

        }
        return rs;
    }
}
